package com.woniuxy.ticketorder.controller;

import com.woniuxy.ticketcommon.entity.ResponseResult;
import com.woniuxy.ticketorder.entity.Order;
import com.woniuxy.ticketorder.exception.OrderAddException;
import com.woniuxy.ticketorder.service.CinemaService;
import com.woniuxy.ticketorder.service.OrderService;
import com.woniuxy.ticketorder.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

/**
 * @author Administrator
 */
@Api("订单接口")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private CinemaService cinemaService;


    /**
     * 添加订单
     * @param order 订单类
     * @return      操作结果
     */
    @ApiOperation("添加订单的接口")
    @PostMapping("/add")
    public ResponseResult orderAdd(@Validated(OrderAddException.class) @RequestBody Order order){
        LocalDateTime now = LocalDateTime.now();
        String str = ""+now.getYear()+now.getMonth()+now.getDayOfMonth()+now.getHour()+now.getMinute()+now.getSecond();
        order.setOrderno("WNDY"+ str);
        order.setOrdertime(now);
        order.setUserid(2);
        order.setMoney(cinemaService.getPrice(1).multiply(new BigDecimal(order.getSeat().split(",").length)));
        order.setPaytime(now.plusMinutes(5L));
        order.setStatus("1");
        orderService.save(order);
        cinemaService.updateseatstatus(Arrays.asList(order.getSeat().split(",")),"1");
        userService.updateUserIntegration(2,order.getMoney().intValue());
        return ResponseResult.SUCCESS;
    }

//    @GetMapping("/add")
//    public String add() {
//        log.debug("Order Add");
//
//        String serviceUser = "http://localhost:9001/user/add";
//
////        try {
////            URL url = new URL(serviceUser);
////            URLConnection urlConnection = url.openConnection();
////            InputStream inputStream = urlConnection.getInputStream();
////            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
////            log.debug(reader.readLine());
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//
//        ResponseEntity<String> forEntity = restTemplate.getForEntity(serviceUser, String.class);
//        System.out.println(forEntity.getBody());
//
//        return "order_add";
//    }

    @ApiOperation("调用用户的接口")
    @ApiImplicitParam(name = "user", value = "调用", defaultValue = "99", required = true)
    @GetMapping("/add2")
    public String add2(){
        return userService.add();
    }


}
